#include "camera.h"
#include <QtDebug>
#include <QtCore>
#include <QImage>
#include "imageloader.h"
#include <QtDeclarative/QDeclarativeImageProvider>
#include "imagepro.h"
#include "type.h"
Camera::Camera(QObject* parent):QObject(parent),m_timer(new QTimer(this))
{
    capwebcame.open(0);

    if(capwebcame.isOpened() == false){
        qDebug()<<"eror :webcamp not accessef successfully";
        return ;
    }
//    capwebcame.set(3,800);
//    capwebcame.set(4,600);
    capwebcame.set(CV_CAP_PROP_FRAME_HEIGHT,cam_h);
    capwebcame.set(CV_CAP_PROP_FRAME_WIDTH,cam_w);
    capwebcame.set(CV_CAP_PROP_FPS,cam_fps);

    connect(m_timer,SIGNAL(timeout()),this,SIGNAL(out()));
    connect(m_timer,SIGNAL(timeout()),this,SLOT(processFrameAndUpdateGUI()));
//     tmrTmier = new QTimer(this);
//     QObject::connect(tmrTmier,SIGNAL(timeout()),this,SLOT(processFrameAndUpdateGUI()));
//     tmrTmier->start(20);
     qDebug()<<"init Camera";

}
void Camera::setInterval(int msec){
    if(m_timer->interval()==msec)
        return;
    m_timer->stop();
    m_timer->setInterval(msec);
    m_timer->start();
    emit intervalChanged();
    qDebug()<< Q_FUNC_INFO<< "intercal ==" << m_timer->interval();

}
int Camera::interval(){
    return m_timer->interval();
}

Camera::~Camera(){
   capwebcame.release();
}
void Camera::setImage(const QVariant &v){
  emit imageChanged();
}
QImage Camera::image(){
    return *qimQriginal;
}

void Camera::Showstatue(void){
    for (int i=0;i<16;i++){
        qDebug()<<i<<capwebcame.get(i);
    }
}
void Camera::processFrameAndUpdateGUI(void){
    capwebcame.read(matOriginal);
    if(matOriginal.empty() == true) return;

    cv::cvtColor(matOriginal,matOriginal,CV_BGRA2RGB);
    qimQriginal = new QImage((uchar*)matOriginal.data,matOriginal.cols ,matOriginal.rows,matOriginal.step,QImage::Format_RGB888);
    Imageloader::getInstance()->setImg(*qimQriginal);
    imp.Pro(150,10);
}

void Camera::cap(){
    capwebcame.read(matOriginal);
     if(matOriginal.empty() == true) return;
    qimQriginal = new QImage((uchar*)matOriginal.data,matOriginal.cols ,matOriginal.rows,matOriginal.step,QImage::Format_RGB888);
  qDebug()<< "Cap";
}

void SaveCapture(QString name){
    //    def SaveCam(self,imagepath):
    //        cv2.imwrite(imagepath,self.f)
   // cv::imwrite(name.toStdString(),);
}

////class Camera(threading.Thread):
////    def __init__(self,callback,family):
////        self.Cam =cv2.VideoCapture(0)
////        self.Cam.set(3,800)
//        self.Cam.set(4,600)
//        self.callback=callback
//        self.family=family


//        for i in range(1,16):
//            print '%s %s' %(i,self.Cam.get(i))

//        threading.Thread.__init__(self)
//    def ReadCam(self):
//        _,self.f=self.Cam.read()
//        _,self.f=self.Cam.read()
//        _,self.f=self.Cam.read()
//    def SaveCam(self,imagepath):
//        cv2.imwrite(imagepath,self.f)
//    def SaveCamSmall(self):
//        self.image_small = cv2.resize(self.f, (200, 200))
//        #        print type (self.image_small)

//        global imgstr
//        ##read form image
//        cv2.imwrite("fish.bmp",self.image_small)
//        with open("fish.bmp","rb") as image_file:
//            encoded_string = base64.b64encode(image_file.read())
//        imgstr = encoded_string

//    #read Camera

//    #
//    #        self.image_small=cv2.cvtColor(self.image_small, cv2.COLOR_BGR2RGB)
//    #        imgstr = base64.b64encode(self.image_small.tostring())

//    def run(self):
//        print  "Camera start"
//        global ex_threadflag
//        i=0
//        while(not ex_threadflag):

//            if (1) :#and (not RfQueue.empty()):

//                print 'QKg recive'
//                global gKG ,gRF ,gImagePath
//                i=i+1
//                # queueLock.acquire()
//                # gKG=str(1)#KgQueue.get()
//                gKG=KgQueue.get()
//                # queueLock.release()
//                gRF=str(2)#RfQueue.get()
//                self.ReadCam()
//                print 'ReadCam'

//                Db = Database('example5.db')
//                fishid=Db.selectFishIDFormRFID(gRF)
//                mid=time.time()
//                gImagePath=str(fishid)+"_"+str(mid)+".jpg"
//                print gImagePath
//                self.SaveCam("img/"+gImagePath)
//                self.SaveCamSmall()

//                p=imagePro(self.f)
//                gX=p.getx()
//                gY=p.gety()
//                Nw.sendSize(gX,gY)


//                p.saveImP("img/"+"imP_"+gImagePath)

//                #                ####image send
//                #                print 'Show image'
//                #                Nw.sendImage();

//                ####
//                print "Call black"
//                self.callback(self.family,gKG,gRF,gImagePath,gX,gY)

//                print "Save Image"
//                Nw.sendFin()
//            #                time.sleep(1)

//        self.Cam.release()
//        print "Camera release"

